package com.example.demo.controller;

import com.example.demo.common.Result;
import com.example.demo.entity.SaleOrder;
import com.example.demo.service.SaleOrderMyBatisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 销售订单MyBatis控制器
 */
@RestController
@RequestMapping("/api/mybatis/sales")
public class SaleOrderMyBatisController {
    
    @Autowired
    private SaleOrderMyBatisService saleOrderMyBatisService;
    
    /**
     * 查询指定时间范围的销售订单
     */
    @GetMapping("/date-range")
    public Result<List<SaleOrder>> findByDateRange(
            @RequestParam LocalDateTime startDate, 
            @RequestParam LocalDateTime endDate) {
        List<SaleOrder> orders = saleOrderMyBatisService.findByDateRange(startDate, endDate);
        return Result.success(orders);
    }
    
    /**
     * 根据收银员ID查询订单
     */
    @GetMapping("/cashier/{cashierId}")
    public Result<List<SaleOrder>> findByCashierId(@PathVariable Long cashierId) {
        List<SaleOrder> orders = saleOrderMyBatisService.findByCashierId(cashierId);
        return Result.success(orders);
    }
    
    /**
     * 查询今日销售统计
     */
    @GetMapping("/stats/today")
    public Result<Map<String, Object>> getTodaySalesStats() {
        Map<String, Object> stats = saleOrderMyBatisService.getTodaySalesStats();
        return Result.success(stats);
    }
    
    /**
     * 查询月度销售统计
     */
    @GetMapping("/stats/monthly")
    public Result<Map<String, Object>> getMonthlySalesStats(
            @RequestParam Integer year, 
            @RequestParam Integer month) {
        Map<String, Object> stats = saleOrderMyBatisService.getMonthlySalesStats(year, month);
        return Result.success(stats);
    }
    
    /**
     * 查询热销商品排行
     */
    @GetMapping("/top-products")
    public Result<List<Map<String, Object>>> getTopSellingProducts(@RequestParam(defaultValue = "10") Integer limit) {
        List<Map<String, Object>> products = saleOrderMyBatisService.getTopSellingProducts(limit);
        return Result.success(products);
    }
    
    /**
     * 根据支付方式统计销售
     */
    @GetMapping("/stats/payment-method")
    public Result<List<Map<String, Object>>> getSalesByPaymentMethod() {
        List<Map<String, Object>> stats = saleOrderMyBatisService.getSalesByPaymentMethod();
        return Result.success(stats);
    }
    
    /**
     * 查询会员消费统计
     */
    @GetMapping("/stats/member-consumption")
    public Result<List<Map<String, Object>>> getMemberConsumptionStats() {
        List<Map<String, Object>> stats = saleOrderMyBatisService.getMemberConsumptionStats();
        return Result.success(stats);
    }
    
    /**
     * 查询销售趋势（按天）
     */
    @GetMapping("/trend/daily")
    public Result<List<Map<String, Object>>> getSalesTrendByDay(@RequestParam(defaultValue = "30") Integer days) {
        List<Map<String, Object>> trend = saleOrderMyBatisService.getSalesTrendByDay(days);
        return Result.success(trend);
    }
    
    /**
     * 查询销售趋势（按小时）
     */
    @GetMapping("/trend/hourly")
    public Result<List<Map<String, Object>>> getSalesTrendByHour(@RequestParam String date) {
        List<Map<String, Object>> trend = saleOrderMyBatisService.getSalesTrendByHour(date);
        return Result.success(trend);
    }
    
    /**
     * 查询退货订单
     */
    @GetMapping("/returns")
    public Result<List<SaleOrder>> findReturnOrders() {
        List<SaleOrder> orders = saleOrderMyBatisService.findReturnOrders();
        return Result.success(orders);
    }
    
    /**
     * 统计指定时间段的销售金额
     */
    @GetMapping("/total-amount")
    public Result<Double> getTotalSalesAmount(
            @RequestParam LocalDateTime startDate, 
            @RequestParam LocalDateTime endDate) {
        Double totalAmount = saleOrderMyBatisService.getTotalSalesAmount(startDate, endDate);
        return Result.success(totalAmount);
    }
}
